﻿function getRootPath() {
	var curWwwPath = window.document.location.href;
	var pathName = window.document.location.pathname;
	var pos = curWwwPath.indexOf(pathName);
	var localhostPaht = curWwwPath.substring(0, pos);
	var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
	return (localhostPaht + projectName);
}


$(function() {
	var Confirm = function(opt) {
		this.defaults = {
			'title_ico' : 'success',
			'title_text' : '操作成功',
			'content_text' : '稍后客服会与您联系',
			onconfirm : function(){},
			oncancel:function(){}
		}, 
		this.options = $.extend({}, this.defaults, opt);
	};
	
	Confirm.prototype = {
			conf : function(confirm) {
				var $back = $("<div id='BgDiv'></div>");
				var $dialog = $("<div id='DialogDiv' style='display: block' class='clearfix p'></div>");
				
				var $middle = $("<div id='middle'></div>");
				var $content = $("<div id='content'></div>");
				var $p = $("<p id='ico'></p>");
				
				var ico_path = "";
				if( this.options.title_ico == 'success'){
					ico_path = '../../common/img/true_icon.png';
				}else if(this.options.title_ico == 'error'){
					ico_path = '../../common/img/error_icon.png';
				}else if(this.options.title_ico == 'warning'){
					ico_path = '../../common/img/warning_icon.png';
				}
				
				var $pimg = $("<img class=\"icon-close\" src='" + ico_path + "' />");
				var $pspan = $("<span>" + this.options.title_text + "</span>");
				$p.append($pimg);
				$p.append($pspan);
				$content.append($p);
				$middle.append($content);
				
				var $foot_text = $("<div id='foot_text' class='clearfix'>" + this.options.content_text + "</div>");
				
				
				var $btn_area = $("<div id='btnArea' class='clearfix' style='position: absolute;width:100%;bottom:20px'></div>");
				var $conf_btn = $("<div style='width:50%;text-align: center;float:left'><a href='#' id='conf'>确定</a></div>");
				var $canc_btn = $("<div style='width:50%;text-align: center;float:left'><a href='#' id='canc'>取消</a></div>");
				
				$btn_area.append($conf_btn);
				$btn_area.append($canc_btn);
				
				$dialog.append($middle);
				$dialog.append($foot_text);
				$dialog.append($btn_area);
				
				$back.css("height",$(document).height());
				
				$("body").append($back);
				$("body").append($dialog);
				
				$("#DialogDiv #btnArea>div>a#conf").click(function(){
					$back.remove();
					$dialog.remove();
					confirm.options.onconfirm();
				});
				
				$("#DialogDiv #btnArea>div>a#canc").click(function(){
					$back.remove();
					$dialog.remove();
					confirm.options.oncancel();
				});
				
			}
		};
	
	var Dialog = function(opt) {
				this.defaults = {
					'title_ico' : 'success',
					'title_text' : '操作成功',
					'content_text' : '稍后客服会与您联系',
					onclose : function(){}
				}, 
				this.options = $.extend({}, this.defaults, opt);
	};
	Dialog.prototype = {
		pop : function(dialog) {
			var $back = $("<div id='BgDiv'></div>");
			var $dialog = $("<div id='DialogDiv' style='display: block' class='clearfix p'></div>");
			
			var $close = $("<div id='close' ></div>");
			var $a_close = $("<a href='#' id='onclose'></a>");
			var $close_img = $("<img class=\"close\" src='../../common/img/close.png' />");
			$a_close.append($close_img);
			$close.append($a_close);
			
			var $middle = $("<div id='middle'></div>");
			var $content = $("<div id='content'></div>");
			var $p = $("<p id='ico'></p>");
			
			var ico_path = "";
			if( this.options.title_ico == 'success'){
				ico_path = '../../common/img/true_icon.png';
			}else if(this.options.title_ico == 'error'){
				ico_path =  '../../common/img/error_icon.png';
			}else if(this.options.title_ico == 'warning'){
				ico_path = '../../common/img/warning_icon.png';
			}
			
			var $pimg = $("<img src='" + ico_path + "' />");
			var $pspan = $("<span>" + this.options.title_text + "</span>");
			$p.append($pimg);
			$p.append($pspan);
			$content.append($p);
			$middle.append($content);
			
			var $foot_text = $("<div id='foot_text' class='clearfix'>" + this.options.content_text + "</div>");
			
			$dialog.append($close);
			$dialog.append($middle);
			$dialog.append($foot_text);
			
			
			$back.css("height",$(document).height());
			
			$("body").append($back);
			$("body").append($dialog);
			
			$("#DialogDiv #close #onclose").click(function(){
				$back.remove();
				$dialog.remove();
				dialog.options.onclose();
			});
			
		}
	};
	$.extend({
		showDialog : function(options) {
			var dialog = new Dialog(options);
			dialog.pop(dialog);
		},
		showConfirm : function(options) {
			var confirm = new Confirm(options);
			confirm.conf(confirm);
		},
		maskLayer : function(flg){
			
			var $maskLayer = $("<div id='maskLayer' style='background-color: #e3e3e3;position: absolute;z-index: 99;left: 0;top: 0;width: 100%;opacity: 0.5;filter: alpha(opacity = 50);-moz-opacity: 0.5;'></div>");
			
			if($("#maskLayer").length == 0){
				$("body").append($maskLayer);
				$maskLayer.css("height",$(document).height());
			}
			
			if(flg == "show"){
				$("#maskLayer").css("display","block");
			}else if(flg == "hide"){
				$("#maskLayer").css("display","none");
			}
		}
	});
})


